﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Forms;


namespace PartsTracker
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private string desktopPath = "C:\\Users\\Branden\\Desktop\\Test.sqlite";
        private string databaseName = string.Empty;
        private bool isNewRecord = false;

        public MainWindow()
        {
            InitializeComponent();

        }

        private void PopulateList()
        {
            this.PartListBox.Items.Clear();

            using (SQLiteConnection db = new SQLiteConnection(desktopPath))
            {
                db.CreateTable<Parts>();
                var parts = db.Query<Parts>("select * from parts");
                foreach (var part in parts)
                {
                    this.PartListBox.Items.Add(part);
                }
            }
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            Stream stream = null;
            OpenFileDialog dialog = new OpenFileDialog();
            // TODO filter to 'sqlite' files

            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                try
                {
                    if ((stream = dialog.OpenFile()) != null)
                    {
                        using (stream)
                        {
                            databaseName = ((FileStream)stream).Name;
                            PopulateList();
                        }
                    }
                }
                catch (Exception ex)
                {
                    System.Windows.Forms.MessageBox
                        .Show("Error: Could not read file from disk. Original error: " +
                        ex.Message);
                }
            }
        }

        private void PartListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var part = this.PartListBox.SelectedItem as Parts;

            if (part != null)
            {
                using (SQLiteConnection db = new SQLiteConnection(desktopPath))
                {
                    var selected = db.Query<Parts>("select * from parts where partname = '" + part.PartName + "'").FirstOrDefault();

                    this.SerialNumberTextBox.Text = selected.PartName;
                    this.HoursTextBox.Text = selected.Hours.ToString();
                }

            }

        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            var selectedPart = this.PartListBox.SelectedItem as Parts;

            using (SQLiteConnection db = new SQLiteConnection(desktopPath))
            {
                if (selectedPart != null)
                {
                    var originalRecord = db.Query<Parts>("select * from parts where partname = '" + selectedPart.PartName + "'");
                    db.Delete<Parts>(originalRecord[0]);
                }

                db.Insert(new Parts
                {
                    PartName = this.SerialNumberTextBox.Text,
                    Hours = Convert.ToInt32(this.HoursTextBox.Text)
                });
            }

            PopulateList();

        }

        private void Button_Click_3(object sender, RoutedEventArgs e)
        {
            isNewRecord = true;
            this.HoursTextBox.Text = string.Empty;
            this.SerialNumberTextBox.Text = string.Empty;
        }





    }
}
